﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using CMN.BLL;
using CMN.Config;
namespace CMN.Controllers
{
    
    [LoggerFilter()]
    [ExceptionFilter()]
    [ExecutionTimingAttribute()]
    public class URLJumpController : Controller
    {
        [CompressFilter]
        [CacheFilterAttribute]
        public string Index(string key)
        {
            URLJumpConfigInfo urlJump = URLJumpConfigFileManager.LoadConfig();
            bool isHas = false;
            for (int i = 0; i < urlJump.URLJumpInfos.Count; i++)
            {
                if (key == urlJump.URLJumpInfos[i].Key)
                {
                    isHas = true;


                    AppLog.Write(Utils.GetUrlReferrer().ToLower() + urlJump.URLJumpInfos[i].ComeUrl, AppLog.LogMessageType.Debug);
                    if (Utils.GetUrlReferrer().ToLower().IndexOf(urlJump.URLJumpInfos[i].ComeUrl) > -1)
                    {

                        string strTemp = Utils.GetFileContent(string.Concat(Utils.GetServerPath(), "App_Data//UrlJump//UrlJump.htm"), Encoding.UTF8);
                        NVelocityHelper nvelocity = nvelocity = new NVelocityHelper(string.Concat(Utils.GetServerPath(), "templates"), true, SiteConfigs.GetSiteConfig().IsCacheTemplate);
                        nvelocity.Put("urljump", urlJump.URLJumpInfos[i]);
                        strTemp = nvelocity.DisplayFromString(strTemp);
                        nvelocity.Dispose();
                        return strTemp;
                    }
                    else
                    {
                       
                        string strTemp = Utils.GetFileContent(string.Concat(Utils.GetServerPath(), "App_Data//UrlJump//showContent.htm"), Encoding.UTF8);
                        NVelocityHelper nvelocity = nvelocity = new NVelocityHelper(string.Concat(Utils.GetServerPath(), "templates"), true, SiteConfigs.GetSiteConfig().IsCacheTemplate);
                        Module_Article bllArticle = new Module_Article();
                        Module_ArticleEntity article = bllArticle.Disp(Utils.StringToInt(urlJump.URLJumpInfos[i].Id, 0));

                        nvelocity.Put("article", article);
                        strTemp = nvelocity.DisplayFromString(strTemp);
                        nvelocity.Dispose();
                        return strTemp;
                       
                    }

                    break;
                }
            }
            if (!isHas)
            {
                Utils.Redirect404();
                return "";
            }
            else
            {
                return "";
            }
        }

        
    }
}
